<?php

namespace App\Console\Commands\model;
;

use App\Dep\SystemDep;
use App\Dep\TextToImageDep;
use App\Dep\VideoTaskDep;
use App\Enum\TextToImageEnum;
use App\Lib\AigcSdk;
use App\Service\UidService;
use Illuminate\Console\Command;

class CheckVideoCmd extends Command
{
    //打标
    public $signature = 'model:check-video';

    public function handle()
    {
        $dep = new TextToImageDep();
        $resWait = $dep->getWaitCheckVideo();
        if ($resWait->isEmpty()) {
            $this->log('have no wait video');
            return false;
        }

        $systemDep = new SystemDep();
        $videoTaskDep = new VideoTaskDep();

        $resSystem = $systemDep->first();
        $resWait->each(function ($item) use ($dep, $videoTaskDep,$resSystem) {
//            if(!$item->video_task_id){
//                return true;
//            }
            $resTask = $videoTaskDep->first($item->video_task_id);
            if($resTask->result ==''){
                return true;
            }

            if($resTask->result == 'fail'){
                $data = [
                    'status' => TextToImageEnum::STATUS_VIDEO_ERR,
                    'video_ok_at' => date('Y-m-d H:i:s'),
                    'video_msg' => $resTask->message,
                ];
                $dep->edit($item->id,$data);
                $this->log("check video error",['id'=> $item->id]);
                return true;
            }

            $data = [
              'status' => TextToImageEnum::STATUS_VIDEO_SUCCESS,
              'video_ok_at' => date('Y-m-d H:i:s')
            ];
            $dep->edit($item->id,$data);
            $this->log("check video success",['id'=> $item->id]);
        });
    }

    private function log($msg, $context = [])
    {
        $this->info("text to image log: {$msg} " . json_encode($context, JSON_UNESCAPED_UNICODE));
        log_daily("check-video")->info($msg, $context);
    }


}
